package com.alibaba.citrus.ots.sales.plan.facade.service.setting;

import com.alibaba.citrus.ots.common.annotation.FacadeInvoker;
import com.alibaba.citrus.ots.common.exception.FacadeException;
import com.alibaba.citrus.ots.sales.plan.api.salesplan.adjust.SalesPlanAdjustDetailFileService;
import com.alibaba.citrus.ots.sales.plan.api.setting.SendMailModuleQueryService;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.CheckVerificationCodeRequest;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.SendMailModuleRequest;
import com.alibaba.citrus.ots.sales.plan.sdo.SendMailModuleSDO;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.lattice2.epoch.middleware.cache.runtime.service.EpochCacheService;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcots.contants.BcOtsConstants;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
public class SendMailModuleQueryServiceImpl implements SendMailModuleQueryService {
    private static final Log log = Log.getLogger(SendMailModuleQueryServiceImpl.class);

    @Resource
    private EpochCacheService epochCacheService;


    @Override
    @FacadeInvoker
    public Result<SendMailModuleSDO> sendMailModule(SendMailModuleRequest sendMailModuleRequest) {
        //判断参数是否传入
        if (StringUtils.isEmpty(sendMailModuleRequest.getUserId())) {
            throw new FacadeException("OTS-02-003-01-16-020");
        }
        if (StringUtils.isEmpty(sendMailModuleRequest.getEmail())) {
            throw new FacadeException("OTS-01-001-01-15-005");
        }

        //随机生成6位验证码
        String verificationCode = (int) ((Math.random() * 9 + 1) * 100000) + "";
        //将验证码存储到redis  有效期30分钟
        String explosionProofBrokenKey = "Explosion_Proof_Password" + sendMailModuleRequest.getUserId();
        try {
            epochCacheService.put(BcOtsConstants.REDIS_CACHE_CODE(), explosionProofBrokenKey, verificationCode, 30L, TimeUnit.MINUTES);
        } catch (Exception e) {
            log.error("保存验证码失败", e);
            throw new FacadeException("OTS-01-001-01-15-015");
        }

        //封装成通知内容返回
        SendMailModuleSDO sendMailModuleSDO = new SendMailModuleSDO();
        sendMailModuleSDO.setRecipient(sendMailModuleRequest.getEmail());
        sendMailModuleSDO.setContent("本次登录验证码为:" + verificationCode + ",30分钟内有效。");
        return Result.success(sendMailModuleSDO);
    }


    @Override
    @FacadeInvoker
    public Result<Boolean> checkVerificationCode(CheckVerificationCodeRequest checkVerificationCodeRequest) {
        if (StringUtils.isEmpty(checkVerificationCodeRequest.getUserId())) {
            throw new FacadeException("OTS-02-003-01-16-020");
        }
        if (StringUtils.isEmpty(checkVerificationCodeRequest.getVerificationCode())) {
            throw new FacadeException("OTS-01-001-01-15-008");
        }

        //获取给该用户发送的验证码
        String explosionProofBrokenKey = "Explosion_Proof_Password" + checkVerificationCodeRequest.getUserId();
        String verificationCode = epochCacheService.get(BcOtsConstants.REDIS_CACHE_CODE(), explosionProofBrokenKey);
        if (StringUtils.isBlank(verificationCode)) {
            throw new FacadeException("OTS-01-001-01-15-011");
        }

        //判断验证码是否一致
        if (!checkVerificationCodeRequest.getVerificationCode().equals(verificationCode)) {
            throw new FacadeException("OTS-01-001-01-15-013");
        }

        //如果一致  返回true 并删除验证码
        epochCacheService.remove(BcOtsConstants.REDIS_CACHE_CODE(), Lists.newArrayList(explosionProofBrokenKey));
        return Result.success(true);
    }
}

